package com.imooc.common.thread.keep;

import cn.hutool.core.thread.ThreadUtil;

import java.util.Map;
import java.util.concurrent.Callable;

public class WorkerTask implements Callable<Map> {

    private volatile Worker worker;

    public WorkerTask(Worker worker) {
        this.worker = worker;
    }


    @Override
    public Map call() throws Exception {

        for (; ; ) {    //自旋操作
            Integer oldV = worker.getCounter().get();
            if (worker.getCounter().compareAndSet(oldV, oldV + 1)){
                System.out.println(Thread.currentThread().getName()+": counter is "+ worker.getCounter().get());
                break;
            }  // CAS操作

        }




        return null;
    }
}
